﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.OA.Portal.Constants;
using NetDon.WebControls;

namespace NetDon.OA.Portal.Controls
{
    public partial class GridPager : BasePager, IPager
    {
        /// <summary>
        /// 获取或设置每页记录数。默认每页15条记录。
        /// </summary>
        public override int PageSize
        {
            get
            {
                return base.PageSize;
            }
            set
            {
                foreach (ListItem item in ddlPageSize.Items)
                {
                    if (string.Compare(value.ToString(), item.Value, true) != 0) continue;
                    base.PageSize = value;
                    return;
                }
                throw new Exception(string.Format("{0} does not support page size {1}.", ID, value));
                // throw new Exception(string.Format(GetResourceString("GetResourceString"),ID,value));
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            bool isLastPage = PageIndex == (PageCount == 0 ? 0 : (PageCount - 1));
            bool isFirstPage = PageIndex == 0;
            lblPageIndex.Text = (PageCount == 0 ? 0 : (PageIndex + 1)) + "/" + PageCount;
            // 设置控件的可用状态
            lblTotal.Text = RecordCount.ToString();
            btnFirst.Enabled = !isFirstPage;
            btnPrev.Enabled = !isFirstPage;
            btnNext.Enabled = !isLastPage;
            btnLast.Enabled = !isLastPage;

            // 设置控件的图像路径

            btnFirst.ImageUrl = ResolveUrl(!isFirstPage ? ApplicationImages.PageFirst : ApplicationImages.PageFirstGray);
            btnPrev.ImageUrl = ResolveUrl(!isFirstPage ? ApplicationImages.PagePrevious : ApplicationImages.PagePreviousGray);
            btnNext.ImageUrl = ResolveUrl(!isLastPage ? ApplicationImages.PageNext : ApplicationImages.PageNextGray);
            btnLast.ImageUrl = ResolveUrl(!isLastPage ? ApplicationImages.PageLast : ApplicationImages.PageLastGray);
            btnFirst.ToolTip = !isFirstPage ? "第一页" : string.Empty;
            btnPrev.ToolTip = !isFirstPage ? "前一页" : string.Empty;
            btnNext.ToolTip = !isLastPage ? "下一页" : string.Empty;
            btnLast.ToolTip = !isLastPage ? "最后一页" : string.Empty;


            lblPageIndex.Text = string.Format("{0}/{1}", PageIndex + 1, PageCount);

            // 加载页码跳转的下拉框
            dropPage.Items.Clear();
            for (int i = 0; i < PageCount; i++)
            {
                dropPage.Items.Add(new ListItem((i + 1).ToString()));
            }
            ddlPageSize.SelectedValue = PageSize.ToString();
            if (dropPage.Items.Count > 0)
            {
                dropPage.SelectedIndex = PageIndex;
            }
            dropPage.Enabled = PageCount > 0;

            base.OnPreRender(e);
        }

        #region Actions

        protected void dropPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            PerformPageQuery(PageSize, dropPage.SelectedIndex);
        }

        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            int pageSize = int.Parse(ddlPageSize.SelectedValue);
            if (pageSize == PageSize) return;
            ViewState["PageSize"] = pageSize;
            PerformPageQuery(pageSize, 0);
        }

        protected void btnNext_Click(object sender, ImageClickEventArgs e)
        {
            PerformPageQuery(PageSize, PageIndex + 1);
        }

        protected void btnPrev_Click(object sender, ImageClickEventArgs e)
        {
            PerformPageQuery(PageSize, PageIndex - 1);
        }

        protected void btnFirst_Click(object sender, ImageClickEventArgs e)
        {
            PerformPageQuery(PageSize, 0);
        }

        protected void btnLast_Click(object sender, ImageClickEventArgs e)
        {
            PerformPageQuery(PageSize, PageCount - 1);
        }

        #endregion
    }
}